<template>
    <div>
        <h3>房价计划列表</h3>
        房价计划名称:
        <input type="text" v-model="can.HousePriceName">
        <input type="button" value="查询" @click="getlist" >
        <input type="button" value="导出" @click="exportFn">
        <input type="file" @change="importfn">
        <input type="button" value="导入" @click="abc">
        <input type="button" value="添加" @click="add">
        <input type="button" value="批量删除" @click="delAll">
        <input type="button"  value="批量开启" @click="updStateAll(1)">
        <input type="button"  value="批量禁用" @click="updStateAll(0)">
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td><input type="checkbox" v-model="checked" @click="checkAll"></td>
                    <td>Id</td>
                    <td>房价计划名称</td>
                    <td>门店</td>
                    <td>房型</td>
                    <td>房型价格</td>
                    <td>数量</td>
                    <td>状态</td>
                    <td>早餐数量</td>
                    <td>开始时间</td>
                    <td>结束时间</td>
                    <td>操作</td>
                </tr>

                <tr v-for="item in data.list">
                    <td><input type="checkbox" v-model="ids" :value="item.Id" @change="checkone"></td>
                    <td>{{item.Id}}</td>
                    <td>{{item.HousePriceName}}</td>
                    <td>{{item.ShopName}}</td>
                    <td>{{item.HouseTypeName}}</td>
                    <td>{{item.Price}}</td>
                    <td>{{item.Count}}</td>
                    <td>{{item.State?"开启":"禁用"}}</td>
                    <td>{{item.BreakFastCount}}</td>
                    <td>{{item.CreateTime}}</td>
                    <td>{{item.EndTime}}</td>
                    <td>
                        <input type="button" value="删除" @click="del(item.Id)">
                        <input type="button" value="修改" @click="upd(item.Id)">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import{ref,reactive,onMounted} from 'vue'
import {useRouter,useRoute} from 'vue-router'
import axios from 'axios';
import moment from 'moment';


const router=useRouter();
const route=useRoute();

const add=()=>{
    router.push('/housePriceAdd')
}

onMounted(()=>{
    getlist();
})

let obj=ref();

const importfn=(e:any)=>{
    obj.value=e.target.files[0];
}
const abc=()=>{
    let f=new FormData();
    f.append('file',obj.value);
    axios({
        method:'post',
        url:'/api/House/ImportHousePrice',
        data:f,

    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert("导入成功");

        }
        else{
            alert("导入失败");
            return;
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

const exportFn=()=>{
    location.href=`https://localhost:7035/api/House/ExportHousePrice?HousePriceName=${can.HousePriceName}`
}
const updStateAll=(state:any)=>{
    if(ids.value.length==0){
        alert("请至少选择一项");
        return;
    }
    if(confirm("确认修改状态吗?")){
        axios({
            method:'put',
            url:'/api/House/HousePricePlanUpdState',
            params:{
                ids:ids.value.toString(),
                state:state
            }
        })
        .then(res=>{
            console.log(res);
            if(res.data>0){
                alert("修改成功");
                checked.value=false;
                ids.value=[];
                getlist();
            }
            else{
                alert("修改失败");
                return;
            }
        })
    }
}
const delAll=()=>{
    if(ids.value.length==0){
        alert("请至少选择一项");
        return;
    }
    if(confirm("确认删除吗?")){
        axios({
            method:'delete',
            url:'/api/House/HousePricePlanDelAll',
            params:{
                ids:ids.value.toString()
            }
        })
        .then(res=>{
            console.log(res);
            if(res.data>0){
                alert("删除成功");
                checked.value=false;
                ids.value=[];
                getlist();
            }
            else{
                alert("删除失败");
                return;
            }
        })
    }
}

const upd=(id:any)=>{
    router.push({
        path:'/housePriceUpd',
        query:{id:id}
    })
}

const del=(id:any)=>{
    if(confirm("确认删除吗?")){
        axios({
            method:'delete',
            url:'/api/House/HousePricePlanDelAll',
            params:{
                ids:id,
            }
        })
        .then(res=>{
            console.log(res);
            if(res.data>0){
                alert("删除成功");
                getlist();
            }
            else{
                alert("删除失败");
                return;
            }
        })
    }
}
const checkone=()=>{
    if(ids.value.length==data.list.length)
{
    checked.value=true;
}
else{
    checked.value=false;
}
}
const checkAll=()=>{
    checked.value=!checked.value
    if(checked.value==true)
{
    ids.value=data.list.map((item:any)=>{
        return item.Id;
    });
}
else{
    ids.value=[];
}
console.log(ids.value)
}
let checked=ref(false);
let ids=ref([]);

const can=reactive({
    HousePriceName:'',
})
const data:any=reactive({
    list:[],
  
})
const getlist=()=>{
    axios({
        method:'get',
        url:'/api/House/HousePricePlanShow',
        params:
            can,
        
    })
    .then(res=>{
        console.log(res);
        data.list=res.data
  

    })
    .catch(err=>{
        console.log(err);
    })
}

</script>

<style scoped>

</style>